package com.punai.customer.controller;

import cn.hutool.core.util.StrUtil;
import com.punai.common.annotation.Log;
import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.core.domain.TreeBo;
import com.punai.common.core.page.TableDataInfo;
import com.punai.common.enums.BusinessType;
import com.punai.common.exception.ServiceException;
import com.punai.common.utils.StringUtils;
import com.punai.customer.domain.CusMechanicalChapter;
import com.punai.customer.domain.contants.CusConstants;
import com.punai.customer.service.ICusMechanicalChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 机械章节Controller
 * 
 * @author zxw
 * @date 2022-12-04
 */
@RestController
@RequestMapping("/cus/mechanicalChapter")
public class CusMechanicalChapterController extends BaseController
{
    @Autowired
    private ICusMechanicalChapterService cusMechanicalChapterService;

    /**
     * 查询机械章节列表
     */
    @PreAuthorize("@ss.hasPermi('cus:mechanicalChapter:list')")
    @GetMapping("/list")
    public TableDataInfo list(CusMechanicalChapter cusMechanicalChapter)
    {
        startPage();
        cusMechanicalChapter.setCompanyId(getCompanyId());
        List<CusMechanicalChapter> list = cusMechanicalChapterService.selectCusMechanicalChapterList(cusMechanicalChapter);
        return getDataTable(list);
    }

    /**
     * 获取机械章节详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(cusMechanicalChapterService.selectCusMechanicalChapterById(id,getCompanyId()));
    }

    /**
     * 新增机械章节
     */
    @PreAuthorize("@ss.hasPermi('cus:mechanicalChapter:add')")
    @Log(title = CusConstants.JX_NAME + "章节", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CusMechanicalChapter cusMechanicalChapter)
    {
        cusMechanicalChapter.setCompanyId(getCompanyId());
        return toAjax(cusMechanicalChapterService.insertCusMechanicalChapter(cusMechanicalChapter));
    }

    /**
     * 修改机械章节
     */
    @PreAuthorize("@ss.hasPermi('cus:mechanicalChapter:edit')")
    @Log(title = CusConstants.JX_NAME + "章节", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CusMechanicalChapter cusMechanicalChapter)
    {
        cusMechanicalChapter.setCompanyId(getCompanyId());
        return toAjax(cusMechanicalChapterService.updateCusMechanicalChapter(cusMechanicalChapter));
    }

    /**
     * 删除机械章节
     */
    @PreAuthorize("@ss.hasPermi('cus:mechanicalChapter:remove')")
    @Log(title = CusConstants.JX_NAME + "章节", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}/{shape}")
    public AjaxResult remove(@PathVariable String id,@PathVariable("shape") String shape) {
        if (StringUtils.isEmpty(id)) {
            throw new ServiceException("章节id获取失败!");
        }
        if (!StrUtil.containsAny(shape,"Y","N")) {
            throw new ServiceException("参数错误!");
        }
        int delMsg = cusMechanicalChapterService.deleteCusMechanicalChapterById(id, getCompanyId(),shape);
        return AjaxResult.success(delMsg);
    }

    @PreAuthorize("@ss.hasPermi('cus:mechanicalChapter:edit')")
    @Log(title = CusConstants.JX_NAME + "章节", businessType = BusinessType.UPDATE)
    @PutMapping("/moveTree/{type}/{id}/{dropId}")
    public AjaxResult moveTree(@PathVariable String type,@PathVariable String id,@PathVariable String dropId)
    {
        cusMechanicalChapterService.moveTree(type, id, dropId,getCompanyId());
        return toAjax(1);
    }

    @PreAuthorize("@ss.hasPermi('cus:mechanicalChapter:list')")
    @GetMapping("/treeData")
    public AjaxResult treeData()
    {
        Long companyId = getCompanyId();
        List<TreeBo> treeData = cusMechanicalChapterService.treeData(companyId);
        return AjaxResult.success(treeData);
    }
}
